/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0  2011-1-4 下午03:09:07
 *
 * PublisherSearchAction.java
 *
 *******************************************************************/
package com.transer.web.action.search;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.transer.constant.Constants;
import com.transer.constant.SQLConstants;
import com.transer.engine.search.WordAnalyzer;
import com.transer.persistance.bean.Project;
import com.transer.persistance.dao.IProjectDao;
import com.transer.utils.StringTools;
import com.transer.utils.Utils;
import com.transer.web.action.TranserActionSupport;

/**
 * class defination are there
 * 
 * @author Mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public class ProjectSearchAction extends TranserActionSupport {
	private static final long serialVersionUID = 5876300572376039001L;
	/** 搜索内容 */
	private String searchTxt;
	// 项目搜索操作
	private IProjectDao projectDao;
	// 查找结果
	private List<Project> projects;
	/** 当前页面 */
	private int page;
	/** 总页数 */
	private int totalPage;
	// 源语言
	private String langFrom;
	// 目标语言
	private String langTo;
	// 专业领域
	private String area;
	// 类型
	private String goodAt;
	// 状态
	private String status;
	// 发布时间
	private String publish;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// /不为空
		String keys = "";
		// 结果数量
		int size = 0;
		// 是否是全文搜索
		boolean isFullText = false;
		// 确切搜索的参数
		Map<String, Object> params = new HashMap<String, Object>();

		params.put(SQLConstants.PROJECT_SEARCH_AREA, Utils.getInteger(area));
		params.put(SQLConstants.PROJECT_SEARCH_DIFF, Utils.getInteger(publish));
		params.put(SQLConstants.PROJECT_SEARCH_LANG_FROM, Utils.getInteger(langFrom));
		params.put(SQLConstants.PROJECT_SEARCH_LANG_TO, Utils.getInteger(langTo));
		params.put(SQLConstants.PROJECT_SEARCH_STATUS, Utils.getInteger(status));
		params.put(SQLConstants.PROJECT_SEARCH_TRANSLATE_TYPE, StringTools.toArray(goodAt, ","));

		params.put(SQLConstants.USER_SEARCH_TYPE, goodAt);
		// 如果是全文搜索
		if (StringUtils.isNotEmpty(searchTxt)) {
			// 全文搜索
			isFullText = true;
			// 加密
			keys = WordAnalyzer.encodeChinese(searchTxt);
			// 结果集数量
			size = projectDao.findProjectSize(keys);
		} else {// 确切参数
			// 不是全文 搜索
			isFullText = false;
			size = projectDao.findProjectSize(params);
		}
		// 总页面数量
		totalPage = new BigDecimal(size).divide(new BigDecimal(Constants.PROJECT_PAGE_SIZE), RoundingMode.UP).intValue();
		// 如果比第一页数量小
		if (page <= 0) {
			page = 1;
		} else if (page > totalPage) {// 超过最后一页
			page = totalPage;
		}
		// 忽略条目数
		int skip = (page - 1) * Constants.PROJECT_PAGE_SIZE;
		// 查找项目
		if (isFullText) {
			projects = projectDao.findProject(keys, skip, Constants.PROJECT_PAGE_SIZE);
		} else {// 确切参数
			projects = projectDao.findProject(params, skip, Constants.PROJECT_PAGE_SIZE);
		}
		// 返回结果
		return TranserActionSupport.SUCCESS;
	}

	/**
	 * @return the searchTxt
	 */
	public String getSearchTxt() {
		return searchTxt;
	}

	/**
	 * @param searchTxt
	 *            the searchTxt to set
	 */
	public void setSearchTxt(String searchTxt) {
		this.searchTxt = searchTxt;
	}

	/**
	 * @return the projectDao
	 */
	public IProjectDao getProjectDao() {
		return projectDao;
	}

	/**
	 * @param projectDao
	 *            the projectDao to set
	 */
	public void setProjectDao(IProjectDao projectDao) {
		this.projectDao = projectDao;
	}

	/**
	 * @return the projects
	 */
	public List<Project> getProjects() {
		return projects;
	}

	/**
	 * @param projects
	 *            the projects to set
	 */
	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}

	/**
	 * @return the page
	 */
	public int getPage() {
		return page;
	}

	/**
	 * @param page
	 *            the page to set
	 */
	public void setPage(int page) {
		this.page = page;
	}

	/**
	 * @return the totalPage
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**
	 * @param totalPage
	 *            the totalPage to set
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	/**
	 * @return the langFrom
	 */
	public String getLangFrom() {
		return langFrom;
	}

	/**
	 * @param langFrom
	 *            the langFrom to set
	 */
	public void setLangFrom(String langFrom) {
		this.langFrom = langFrom;
	}

	/**
	 * @return the langTo
	 */
	public String getLangTo() {
		return langTo;
	}

	/**
	 * @param langTo
	 *            the langTo to set
	 */
	public void setLangTo(String langTo) {
		this.langTo = langTo;
	}

	/**
	 * @return the area
	 */
	public String getArea() {
		return area;
	}

	/**
	 * @param area
	 *            the area to set
	 */
	public void setArea(String area) {
		this.area = area;
	}

	/**
	 * @return the goodAt
	 */
	public String getGoodAt() {
		return goodAt;
	}

	/**
	 * @param goodAt
	 *            the goodAt to set
	 */
	public void setGoodAt(String goodAt) {
		this.goodAt = goodAt;
	}

	/**
	 * @return the status
	 */
	public String getStatus() {
		return status;
	}

	/**
	 * @param status
	 *            the status to set
	 */
	public void setStatus(String status) {
		this.status = status;
	}

	/**
	 * @return the publish
	 */
	public String getPublish() {
		return publish;
	}

	/**
	 * @param publish
	 *            the publish to set
	 */
	public void setPublish(String publish) {
		this.publish = publish;
	}
}